# -*- coding: utf-8 -*-
# vim: ft=jinja

{%- set tplroot = tpldir.split('/')[0] %}
{%- import_yaml tplroot ~ "/defaults.yaml" as default_settings with context %}
{%- import_yaml tplroot ~ "/osarchmap.yaml" as osarchmap with context %}
{%- import_yaml tplroot ~ "/osfamilymap.yaml" as osfamilymap with context %}
{%- import_yaml tplroot ~ "/osmap.yaml" as osmap with context %}
{%- import_yaml tplroot ~ "/osfingermap.yaml" as osfingermap with context %}
{%- import_yaml tplroot ~ "/oscodenamemap.yaml" as oscodenamemap with context %}

{%- set _config = salt['config.get'](tplroot, default={}) %}
{%- set defaults = salt['grains.filter_by'](
      default_settings,
      default=tplroot,
      merge=salt['grains.filter_by']( osarchmap, grain='osarch',
        merge=salt['grains.filter_by']( osfamilymap, grain='os_family',
          merge=salt['grains.filter_by']( osmap, grain='os',
            merge=salt['grains.filter_by']( osfingermap, grain='osfinger',
              merge=salt['grains.filter_by']( oscodenamemap, grain='oscodename',
                merge=salt['grains.filter_by']( _config, default='lookup'
                )
              )
            )
          )
        )
      )
    )
%}
{%- set node = salt['grains.filter_by']( {'defaults': defaults}, default='defaults', merge=_config) %}

{%- if node.pkg.use_upstream_source %}
    {%- do node.pkg.source.update({ 'name': node.dir.source ~ '/node-' ~ node.version,
           'source': '%s/v%s.%s'|format(node.pkg.source.uri, node.version, node.pkg.suffix) }) %}
{%- endif %}
{%- if node.pkg.use_upstream_archive %}
    {%- set dirname = 'node-v%s-%s-%s'|format( node.version, node.kernel, node.arch ) %}
    {%- do node.pkg.archive.update({
           'name': node.dir.archive ~ '/' ~ dirname,
           'source_hash': node.pkg.archive.uri ~ '/v' ~ node.version ~ '/SHASUMS256.txt',
           'source': '%s/v%s/%s.%s'|format(node.pkg.archive.uri, node.version, dirname, node.pkg.suffix)}) %}
{%- endif %}
{%- if node.pkg.use_upstream_repo -%}
    {%- set repo_version = node.version.split('.')[0] | default('') %}
    {%- set full_repo_name = 'deb %s/node_%s.x %s'|format(node.pkg.repo.url, repo_version, node.pkg.repo.dist) -%}
    {%- do node.pkg.repo.update({
           'name': full_repo_name}) -%}
{%- endif -%}
